﻿using QuanLyThuVien.DTO;
using QuanLyThuVien.Supporter;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace QuanLyThuVien.DAL
{
    //----------------------------------------------------------------------
    //Desc: class kết nối với bảng CT_TACGIA
    //----------------------------------------------------------------------
    public class DALCT_TacGia
    {
        //----------------------------------------------------------------------
        //Desc: constructor mặc định
        //----------------------------------------------------------------------
        public DALCT_TacGia()
        { }

        //----------------------------------------------------------------------
        //Desc: chuyển dòng dữ liệu thành CT_TacGia
        //----------------------------------------------------------------------
        public DTOCT_TacGia ConvertToCT_TacGia(DataRow row)
        {
            DTOCT_TacGia cttg = new DTOCT_TacGia();
            cttg.MaDauSach = row["MaDauSach"].ToString();
            cttg.TacGia.MaTG = row["MaTG"].ToString();
            return cttg;
        }

        //----------------------------------------------------------------------
        //Desc: Lấy danh sách tên tác giả
        //----------------------------------------------------------------------
        public List<string> LayDSTenTG(string madausach)
        {
            if (DatabaseManager.DbConnection == null)
                return null;
            string spName = "spCT_TacGia_LayDanhSachTenTG";
            SqlParameter sqlprMaDauSach = new SqlParameter("@MaDauSach", SqlDbType.VarChar, 15) { Value = madausach };
            DataTable data = DatabaseManager.DbConnection.ExecuteStoredProcedure(spName, sqlprMaDauSach);
            if (data == null)
                return null;
            List<string> list = new List<string>();
            for (int i = 0; i < data.Rows.Count; i++)
                list.Add(data.Rows[i]["TenTacGia"].ToString());
            return list;
        }

        //----------------------------------------------------------------------
        //Desc: lấy danh sách tác giả
        //----------------------------------------------------------------------
        public List<DTOCT_TacGia> LayDSCT_TacGia(string madausach)
        {
            if (DatabaseManager.DbConnection == null)
                return null;
            string spName = "spCT_TacGia_LayDanhSachCTTG";
            SqlParameter sqlprMaDauSach = new SqlParameter("@MaDauSach", SqlDbType.VarChar, 15) { Value = madausach };
            DataTable data = DatabaseManager.DbConnection.ExecuteStoredProcedure(spName, sqlprMaDauSach);
            if (data == null)
                return null;
            List<DTOCT_TacGia> list = new List<DTOCT_TacGia>();
            foreach (DataRow row in data.Rows)
                list.Add(ConvertToCT_TacGia(row));
            return list;
        }

        //----------------------------------------------------------------------
        //Desc: đếm CT_Tacgia
        //----------------------------------------------------------------------
        public int DemCTTacGia(DTOCT_TacGia cttg)
        {
            if (DatabaseManager.DbConnection == null)
                return -1;
            string spName = "spCT_TacGia_DemCTTG";
            SqlParameter sqlprMaDauSach = new SqlParameter("@MaDauSach", SqlDbType.VarChar, 15) { Value = cttg.MaDauSach };
            SqlParameter sqlprMaTG = new SqlParameter("@MaTG", SqlDbType.VarChar, 15) { Value = cttg.TacGia.MaTG };
            object obj = DatabaseManager.DbConnection.ExecuteStoredProcedureScalar(spName, sqlprMaDauSach, sqlprMaTG);
            if (obj == null)
                return -1;
            int dem;
            try
            {
                dem = (int)obj;
            }
            catch { dem = -1; }
            return dem;
        }

        //----------------------------------------------------------------------
        //Desc: cập nhât CT_TacGia
        //----------------------------------------------------------------------
        public bool CapNhat(DTOCT_TacGia cttg)
        {
            if (DatabaseManager.DbConnection == null)
                return false;
            string spName = "spCT_TacGia_CapNhatCTTG";
            SqlParameter sqlprThaoTac = new SqlParameter("@ThaoTac", SqlDbType.Int) { Value = 1 };
            SqlParameter sqlprMaDauSach = new SqlParameter("@MaDauSach", SqlDbType.VarChar, 15) { Value = cttg.MaDauSach };
            SqlParameter sqlprMaTG = new SqlParameter("@MaTG", SqlDbType.VarChar, 15) { Value = cttg.TacGia.MaTG };
            return DatabaseManager.DbConnection.ExecuteStoredProcedureNonQuery(spName, sqlprThaoTac, sqlprMaDauSach, sqlprMaTG);
        }

        //----------------------------------------------------------------------
        //Desc: Xóa CT_TacGia
        //----------------------------------------------------------------------
        public bool Xoa(DTOCT_TacGia cttg)
        {
            if (DatabaseManager.DbConnection == null)
                return false;
            string spName = "spCT_TacGia_CapNhatCTTG";
            SqlParameter sqlprThaoTac = new SqlParameter("@ThaoTac", SqlDbType.Int) { Value = 0 };
            SqlParameter sqlprMaDauSach = new SqlParameter("@MaDauSach", SqlDbType.VarChar, 15) { Value = cttg.MaDauSach };
            SqlParameter sqlprMaTG = new SqlParameter("@MaTG", SqlDbType.VarChar, 15) { Value = cttg.TacGia.MaTG };
            return DatabaseManager.DbConnection.ExecuteStoredProcedureNonQuery(spName, sqlprThaoTac, sqlprMaDauSach, sqlprMaTG);
        }
    }
}
